﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IsreversI.GUI
{
    class ScoreControl : UserControl
    {
        #region membervars
        #region GUI elements
        private PictureBox redStoneBox;
        private PictureBox blueStoneBox;
        private Label redScoreLabel;
        private Label blueScoreLabel;
        private ListBox gameStatusListBox;
        private Label gameStatusLabel;

        private static readonly Size defaultBoxSize = new Size(30, 30);
        #endregion

        private GameLogic.GameStateView stateView;
        #endregion

        #region events
        #endregion

        #region properties
        #endregion

        #region constructors
        public ScoreControl(GameLogic.GameStateView stateView)
        {
            this.stateView = stateView;
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.redStoneBox = new PictureBox();
            this.blueStoneBox = new PictureBox();
            this.redScoreLabel = new Label();
            this.blueScoreLabel = new Label();
            this.gameStatusListBox = new ListBox();
            this.gameStatusLabel = new Label();
            this.SuspendLayout();
            // 
            // redStoneBox
            // 
            {
                this.redStoneBox.Location = new System.Drawing.Point(10, 15);
                Image redImage = new Bitmap(defaultBoxSize.Width, defaultBoxSize.Height);
                Graphics gr = Graphics.FromImage(redImage);
                gr.DrawImage(IsreversI.Properties.Resources.redstone, new Rectangle(new Point(0, 0), defaultBoxSize));
                this.redStoneBox.Image = redImage;
                this.redStoneBox.Size = ScoreControl.defaultBoxSize;
            }
            // 
            // blueStoneBox
            // 
            {
                this.blueStoneBox.Location = new System.Drawing.Point(10, 65);
                Image blueImage = new Bitmap(defaultBoxSize.Width, defaultBoxSize.Height);
                Graphics gr = Graphics.FromImage(blueImage);
                gr.DrawImage(IsreversI.Properties.Resources.bluestone, new Rectangle(new Point(0, 0), defaultBoxSize));
                this.blueStoneBox.Image = blueImage;
                this.blueStoneBox.Size = ScoreControl.defaultBoxSize;
            }
            // 
            // redScoreLabel
            // 
            this.redScoreLabel.AutoSize = true;
            this.redScoreLabel.Location = new System.Drawing.Point(50, 25);
            this.redScoreLabel.Text = "2 points";
            // 
            // blueScoreLabel
            // 
            this.blueScoreLabel.AutoSize = true;
            this.blueScoreLabel.Location = new System.Drawing.Point(50, 70);
            this.blueScoreLabel.Text = "2 points";

            // 
            // gameStatusListBox
            //
            this.gameStatusListBox.Size = new Size(200, 80);
            this.gameStatusListBox.Location = new Point(110, 25);
            this.gameStatusListBox.BackColor = SystemColors.Control;
            this.gameStatusListBox.BorderStyle = BorderStyle.FixedSingle;
            this.gameStatusListBox.Items.Add("A new game starts.");
            //
            // gameStatusLabel
            //
            this.gameStatusLabel.Text = "Game status:";
            this.gameStatusLabel.Location = new Point(110, 8);
            this.gameStatusLabel.Font = new Font(this.gameStatusLabel.Font, FontStyle.Bold);
            //
            // ScoreControl
            // 
            this.stateView.GameOver += this.handleGameOver;
            this.stateView.ScoreUpdated += this.handleScoreUpdate;
            this.stateView.PlayerHasNoMoves += this.handleSkipTurn;
            this.stateView.PostBoardModelChanged += this.handleBoardChange;
            this.Controls.Add(this.blueScoreLabel);
            this.Controls.Add(this.redScoreLabel);
            this.Controls.Add(this.blueStoneBox);
            this.Controls.Add(this.redStoneBox);
            this.Controls.Add(this.gameStatusListBox);
            this.Controls.Add(this.gameStatusLabel);
            this.Size = new System.Drawing.Size(320, 120);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        #region event handlers
        private void handleBoardChange(object sender, GameLogic.BoardEventArgs bea)
        {
            this.gameStatusListBox.Items.Clear();
            this.gameStatusListBox.Items.Add("A new game starts.");
            this.redScoreLabel.Text = this.blueScoreLabel.Text = "2 points";
        }

        private void handleGameOver(object sender, GameLogic.BoardEventArgs bea)
        {
            if (stateView.RedPoints > stateView.BluePoints)
                this.gameStatusListBox.Items.Add("The game is won by Red.");
            else if (stateView.BluePoints > stateView.RedPoints)
                this.gameStatusListBox.Items.Add("The game is won by Blue.");
            else
                this.gameStatusListBox.Items.Add("The game ends in a draw.");
            this.gameStatusListBox.SetSelected(this.gameStatusListBox.Items.Count - 1, true);
            this.gameStatusListBox.ClearSelected();
        }

        private void handleScoreUpdate(object sender, GameLogic.BoardEventArgs bea)
        {
            this.redScoreLabel.Text = this.stateView.RedPoints.ToString();
            this.blueScoreLabel.Text = this.stateView.BluePoints.ToString();
            if (!bea.OldState.HasFlag(bea.NewState.Flipped()))
            {
                this.gameStatusListBox.Items.Add("A stone is placed on " + (char)(bea.Point.X + 'A') + (bea.Point.Y + 1).ToString() + " by " + bea.NewState.ToString() + ".");
                //Sets the scroll position to the last item of the listbox.
                this.gameStatusListBox.SetSelected(this.gameStatusListBox.Items.Count - 1, true);
                this.gameStatusListBox.ClearSelected();
            }
        }

        private void handleSkipTurn(object sender, GameLogic.BoardEventArgs bea)
        {
            if (bea.BoardModel.CurrentState == TurnState.GameOver)
                return;
            this.gameStatusListBox.Items.Add("A turn is skipped by " + bea.BoardModel.CurrentState.Flipped() + ".");
            this.gameStatusListBox.SetSelected(this.gameStatusListBox.Items.Count - 1, true);
            this.gameStatusListBox.ClearSelected();
        }
        #endregion

        #region public methods
        #endregion

        #region private methods
        #endregion
    }
}
